import 'dart:io';

import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';

enum RouteTransition { fade, zoom, rotation, slide }

class UtilsNavigator {
  static Future<T?>  push<T>(BuildContext context, page,
      {Function? callback, bool fullscreenDialog = false}) async {
    if (Platform.isIOS||Platform.isMacOS|| Platform.isWindows) {
      return Navigator.push(
          context,
          MaterialPageRoute(
              builder: (BuildContext context) => page,
              fullscreenDialog: fullscreenDialog));
    } else if (Platform.isAndroid) {
      // 自定义路由动画
      return Navigator.push(
          context,
          CustomRouter(page,
              duration: 200,
              transition: fullscreenDialog
                  ? RouteTransition.slide
                  : RouteTransition.fade,
              fullscreen: fullscreenDialog));
    }
    return null;
  }

  static void pop<T>(BuildContext context, {T? arguments}) {
    Navigator.pop(context, arguments);
  }

  static void pushAndRemove(BuildContext context, page) {
    Navigator.pushAndRemoveUntil(
        context,
        MaterialPageRoute(builder: (BuildContext context) => page),
        (route) => false);
  }

  static void pushAndReplace(BuildContext context, page,
      {bool fullscreenDialog = false}) {
    Navigator.pushReplacement(
        context,
        MaterialPageRoute(
            builder: (BuildContext context) => page,
            fullscreenDialog: fullscreenDialog));
  }

  static openLaunch(Uri url) async {
    try {
      await launchUrl(url);
    } catch (e) {
      throw '不能打开 $e';
    }
  }
}

class CustomRouter<T> extends PageRouteBuilder<T> {
  final Widget widget; // 目标页面
  final RouteTransition? transition; // 动画效果
  final int? duration; // 动画时长
  final bool fullscreen;

  CustomRouter(this.widget,
      {this.transition, this.duration, this.fullscreen = false})
      : super(
          // 构造器
          pageBuilder: (BuildContext context, Animation<double> animation1,
              Animation<double> animation2) {
            return widget;
          },
          // 过渡时间
          transitionDuration: Duration(milliseconds: duration ?? 1000),
          transitionsBuilder: (BuildContext context,
              Animation<double> animation1,
              Animation<double> animation2,
              Widget child) {
            if (transition == RouteTransition.fade) {
              // 淡入
              return FadeTransition(
                opacity: Tween(begin: 0.0, end: 1.0).animate(CurvedAnimation(
                    parent: animation1, curve: Curves.fastOutSlowIn)),
                child: child,
              );
            } else if (transition == RouteTransition.zoom) {
              // 缩放
              return ScaleTransition(
                scale: Tween(begin: 0.0, end: 1.0).animate(CurvedAnimation(
                    parent: animation1, curve: Curves.fastOutSlowIn)),
                child: child,
              );
            } else if (transition == RouteTransition.rotation) {
              // 旋转
              return RotationTransition(
                turns: Tween(begin: 0.0, end: 1.0).animate(CurvedAnimation(
                    parent: animation1, curve: Curves.fastOutSlowIn)),
                child: ScaleTransition(
                  scale: Tween(begin: 0.0, end: 1.0).animate(CurvedAnimation(
                      parent: animation1, curve: Curves.fastOutSlowIn)),
                  child: child,
                ),
              );
            } else {
              // 左滑进入// fullscreenDialog=true底部弹出进入
              return SlideTransition(
                  position: Tween<Offset>(
                          begin: fullscreen
                              ? const Offset(0.0, 1.0)
                              : const Offset(1.0, 0.0),
                          end: const Offset(0.0, 0.0))
                      .animate(CurvedAnimation(
                          parent: animation1, curve: Curves.fastOutSlowIn)),
                  child: child);
            }
          },
        );
}
